<?php
/**
*date: 2016年9月26日
*/
namespace app\admin\controller;
use think\Db;
use app\common\controller\AdminBase;
class Sys extends AdminBase{
		/*index*/
		public function index(){
		    $data=Db::name('auth_rule')->select();
		   $data= AdminBase::tree($data);		  
		    $this->assign('list',$data);
			return $this->fetch();
		}	
		/*add*/
		public function add(){		    
		    $tree=Db::name('auth_rule')->select();
		    $tree= AdminBase::tree($tree);
		    $this->assign('tree',$tree);
		    if(request()->isPost()){
		        $data=input('post.');
		        $data['id']=null;
		            $id=Db::name('auth_rule')->insert($data,false,true);
		            if($id){
		                return "新增成功！";
		            }else{
		                return "新增失败！";
		            }		        
		    }
			return $this->fetch();
		}
		/*edit*/
		public function edit(){
		    $tree=Db::name('auth_rule')->select();
		    $tree= AdminBase::tree($tree);
		    $this->assign('tree',$tree);
		    $id=input('cid');
		    $this->assign("editData",Db::name('auth_rule')->where('id',$id)->find());
		    if(request()->isPost()){
		        $data=input('post.');
		            $id=Db::name('auth_rule')->update($data,false,true);
		            if($id){
		                return "修改成功！";
		            }else{
		                return "修改失败！";
		            }	       
		        
		    }
		    return $this->fetch();
		}
		
		/*delete*/
		public function delete(){
		    if(request()->isPost()){
		        $id=input('id');
		        $data=Db::name('auth_rule')->column('pid');
		        if(in_array($id,$data)){
		            return ['msg'=>"有下级资源，删除失败！"];
		        }else{
		            $result=Db::name('auth_rule')->where('id',$id)->delete();		           
		            if($result){
		                return ['msg'=>"删除成功！"];
		            }else{
		                return ['msg'=>'删除失败！'];
		            }
		        }
		    }else{
		        return $this->fetch();
		    }
			
		}
		
}
?>